package cn.jinjimi.app.main.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.R;
import cn.jinjimi.app.controller.UserController;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.UploadProfileEvent;
import cn.jinjimi.app.entity.Account;
import cn.jinjimi.app.entity.UserProfiles;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by m1 on 15/1/27.
 */
public class ModifyGenderActivity extends ToolBarCommonActivity {

    private static final String TAG = ModifyGenderActivity.class.getSimpleName();

    private RelativeLayout maleLayout;

    private RelativeLayout femaleLayout;

    private ImageView maleView;

    private ImageView femaleView;

    private int gender;

    private int oldGender;

    public static void launchMe(Context startActivity) {
        if (startActivity == null)
            return;
        Intent intent = new Intent(startActivity, ModifyGenderActivity.class);
        if (!(startActivity instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        startActivity.startActivity(intent);
    }

    public static void launchMeForResult(Context startActivity, int requestCode) {
        if (startActivity == null)
            return;
        Intent intent = new Intent(startActivity, ModifyGenderActivity.class);
        if (!(startActivity instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        ((Activity) startActivity).startActivityForResult(intent, requestCode);
    }

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activtiy_modify_gender);
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        setTitle(getString(R.string.info_gender));
        maleLayout = (RelativeLayout) findViewById(R.id.rl_gender_male);
        femaleLayout = (RelativeLayout) findViewById(R.id.rl_gender_female);
        maleView = (ImageView) findViewById(R.id.male_checked);
        femaleView = (ImageView) findViewById(R.id.female_checked);
    }

    protected void initListener() {
        maleLayout.setOnClickListener(this);
        femaleLayout.setOnClickListener(this);
    }

    protected void initData() {
        Account account = AccountManager.getInstance().getAccount();
        gender = account.gender;
        oldGender = gender;
        setGender(gender == UserProfiles.GENDER_MALE);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.rl_gender_male:
                setGender(true);
                break;
            case R.id.rl_gender_female:
                setGender(false);
                break;
        }
        if (gender != oldGender) {
            upgradeGender();
        } else {
            finish();
        }
    }

    public void upgradeGender() {
        showLoadingIndicator();
        UserController.setGender(gender);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUploadProfileEvent(UploadProfileEvent event) {
        if (isFinishing())
            return;
        switch (event.state) {
            case DataEvent.SUCC:
                AccountManager.getInstance().updateAccount(AccountManager.NameKey.GENDER, gender);
                ToastUtils.toastMessage(ModifyGenderActivity.this, getString(R.string.modify_success));
                setResult(RESULT_OK);
                finish();
                break;
            case DataEvent.FAIL:
                ToastUtils.toastMessage(ModifyGenderActivity.this, getString(R.string.modify_fail));
                break;
        }
        hideLoadingIndicator();
    }

    private void setGender(boolean isMale) {
        if (isMale) {
            maleView.setVisibility(View.VISIBLE);
            femaleView.setVisibility(View.INVISIBLE);
            gender = UserProfiles.GENDER_MALE;
        } else {
            maleView.setVisibility(View.INVISIBLE);
            femaleView.setVisibility(View.VISIBLE);
            gender = UserProfiles.GENDER_FEMALE;
        }
    }
}
